package com.example.a2205_fx.register;

import androidx.appcompat.app.AppCompatActivity;
import androidx.lifecycle.Observer;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import com.blankj.utilcode.util.ToastUtils;
import com.example.a2205_fx.MainActivity;
import com.example.a2205_fx.R;
import com.example.a2205_fx.base.BaseActivity;
import com.example.a2205_fx.databinding.ActivityRegisterBinding;
import com.example.a2205_fx.entity.UserEntity;

public class RegisterActivity extends BaseActivity<ActivityRegisterBinding,RegisterViewModel> {



    @Override
    protected void initData() {
        viewModel.success.observe(this, new Observer<UserEntity>() {
            @Override
            public void onChanged(UserEntity entity) {
                ToastUtils.showLong(entity.getMessage());
                if(entity.getCode()==200){
                    Intent intent = new Intent(RegisterActivity.this, MainActivity.class);
                    intent.putExtra("username",entity.getData().getUsername());
                    intent.putExtra("password",entity.getData().getPassword());
                    startActivity(intent);
                }
            }
        });
    }

    @Override
    protected void initView() {
        binding.register.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String string = binding.username.getText().toString();
                String string1 = binding.password.getText().toString();
                String string2 = binding.repassword.getText().toString();
                viewModel.register(string,string1,string2);
            }
        });

    }

    @Override
    protected Class<RegisterViewModel> getViewModel() {
        return RegisterViewModel.class;
    }

    @Override
    protected int getLayout() {
        return R.layout.activity_register;
    }
}